package com.hy.admin.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hy.admin.entity.AnswerEntity;
import com.hy.admin.entity.QuestionEntity;
import com.hy.admin.helper.request.AnswerAddRequest;
import com.hy.admin.helper.request.QuestionRequest;
import com.hy.admin.helper.response.AnswerResponse;
import com.hy.admin.helper.response.QuestionResponse;
import com.hy.admin.service.AnswerService;
import com.hy.admin.service.QuestionService;
import com.hy.core.request.ApiRequestDTO;
import com.hy.core.response.ApiResponseDTO;
import com.hy.core.response.ServicesResult;
import org.apache.commons.httpclient.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by yangqq on 18/4/5.
 * 个人中心
 */
@RestController
@RequestMapping("/api/answer")
public class AnswerController {

    protected final Logger logBase = LoggerFactory.getLogger(AnswerController.class);
    @Autowired
    private AnswerService answerService;

    /**
     * 回答
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponseDTO add(ApiRequestDTO apiRequestDTO,@RequestParam(value = "files",required = false) MultipartFile[] files) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("answer_add_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        AnswerAddRequest answerAddRequest= gson.fromJson(apiRequestDTO.getRequestData(), AnswerAddRequest.class);

        //获取数据
        ServicesResult<Boolean> servicesResult = null;
        try {
            servicesResult = answerService.add(answerAddRequest, answerAddRequest.getTeacherId(), files);
        } catch (Exception e) {
            e.printStackTrace();
            servicesResult = new ServicesResult<>();
            servicesResult.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
            servicesResult.setStatusMsg("系统异常");
        }
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("answer_add_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }

    /**
     * 查看答案
     * @param apiRequestDTO
     * @return
     */
    @RequestMapping(value = "/show", method = RequestMethod.POST)
    public ApiResponseDTO show(@RequestBody final ApiRequestDTO apiRequestDTO) {

        Gson gson = new GsonBuilder().disableHtmlEscaping().create();
        //打印请求参数
        logBase.info("answer_show_request:{}", gson.toJson(apiRequestDTO));
        ApiResponseDTO apiResponseDTO = new ApiResponseDTO(HttpStatus.SC_ACCEPTED, "", apiRequestDTO.getUuid());

        //获取请求对象
        QuestionRequest questionRequest= gson.fromJson(apiRequestDTO.getRequestData(), QuestionRequest.class);

        //获取数据
        ServicesResult<AnswerResponse> servicesResult = answerService.show(questionRequest.getQuestionId(), questionRequest.getMemberId());
        apiResponseDTO.initServicesResult(servicesResult);
        //处理返回对象
        apiResponseDTO.setResponseData(gson.toJson(servicesResult.getResult()));
        logBase.info("answer_show_response:{}", gson.toJson(apiResponseDTO));
        return apiResponseDTO;
    }
}
